import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Message } from "src/entity/message.entity";
import { messageData } from "src/interface/messageType";
import { WsGateway } from "src/ws/ws.gateway";
import { Repository } from "typeorm";

@Injectable()
export class MessageService {
  constructor(
    private ws: WsGateway,
    @InjectRepository(Message) private messageEntity: Repository<Message>
  ) {}
  async sendMessage(room_id: number, message: messageData) {
    const state = await this.messageEntity.save(message);
    this.ws.send(room_id.toString(), message);
  }
  async getHistory(room_id: number) {
    const history = await this.messageEntity.find({
      order: { message_id: "DESC" },
      where: {
        message_room: room_id,
      },
      take: 100,
    });
    return history;
  }
}
